﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;
using System.ComponentModel;
using System.ComponentModel.Design;

using edowork.com.SQL2005;


namespace WebInvoicingsys.basecontrol.business
{
    public partial class uc_from_product : System.Web.UI.UserControl
    {
        public string getValue()
        {
            return this.txt_product.Text;
        }

        public void clearValue()
        {
            this.txt_product.Text = "";
        }

        //获取指字符个数的字符 
        public string cuts(string aa, int bb)
        {
            if (aa.Length <= bb) { return aa; }
            else { return aa.Substring(0, bb); } 
        }

        //Repeater分页控制显示方法
        public void contrlRepeater()
        {
            Execute exec = new Execute(Session["connectionString"].ToString());

            //构造SQL语句
            string sql = @"select a.productcode, a.productname, a.productshortname, a.maxqty, a.minqty, b.goodsclassname, c.goodsunitname, d.goodsspecname, e.stackname, f.guardname, a.sysdate, sum(g.qty1) Qty1
                              from  product a, goodsclass b, goodsunit c, goodsspec d, stack e, guard f, inventory g
                                where a.goodsclassid=b.goodsclassid
                                  and a.goodsunitid=c.goodsunitid
                                  and a.goodsspecid=d.goodsspecid
                                  and a.stackid=e.stackid
                                  and a.guardid=f.guardid
                                  and a.productcode=g.productcode";

            string error_msg = "";

            string productcode = txt_productcode.Text.Trim();
            string productname = txt_productname.Text.Trim();
            string productshortname = txt_productshortname.Text.Trim();

            sql += (productcode == "") ? "" : String.Format(" and a.productcode like '{0}%'", productcode);
            sql += (productname == "") ? "" : String.Format(" and a.productname like '{0}%'", productname);
            sql += (productshortname == "") ? "" : String.Format(" and a.productshortname like '{0}%'", productshortname);

            sql += " group by a.productcode, a.productname, a.productshortname, a.maxqty, a.minqty, b.goodsclassname, c.goodsunitname, d.goodsspecname, e.stackname, f.guardname, a.sysdate";

            DataSet ds = exec.ExecSQLQueryDataSet(sql, ref error_msg);

            if (ds == null)
            {
                return;
            }

            DataSet dsResult = new DataSet();

            //绑定菜单
            DataTable dt = new DataTable("List");

            dt.Columns.Add("dgvID");
            dt.Columns.Add("productcode");
            dt.Columns.Add("productname");
            dt.Columns.Add("productshortname");
            dt.Columns.Add("goodsclassname");
            dt.Columns.Add("goodsunitname");
            dt.Columns.Add("goodsspecname");
            dt.Columns.Add("stackname");
            dt.Columns.Add("guardname");
            dt.Columns.Add("maxqty");
            dt.Columns.Add("minqty");
            dt.Columns.Add("sysdate");
            dt.Columns.Add("Qty1");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                DataRow drnew = dt.NewRow();

                drnew["dgvID"] = (dt.Rows.Count + 1).ToString();
                drnew["productcode"] = dr["productcode"].ToString();
                drnew["productname"] = dr["productname"].ToString();
                drnew["productshortname"] = dr["productshortname"].ToString();
                drnew["goodsclassname"] = dr["goodsclassname"].ToString();
                drnew["goodsunitname"] = dr["goodsunitname"].ToString();
                drnew["goodsspecname"] = dr["goodsspecname"].ToString();
                drnew["stackname"] = dr["stackname"].ToString();
                drnew["guardname"] = dr["guardname"].ToString();
                drnew["maxqty"] = dr["maxqty"].ToString();
                drnew["minqty"] = dr["minqty"].ToString();
                drnew["sysdate"] = dr["sysdate"].ToString();
                drnew["Qty1"] = dr["Qty1"].ToString();

                dt.Rows.Add(drnew);
            }

            dsResult.Tables.Add(dt);

            PagedDataSource pds = new PagedDataSource();

            pds.DataSource = dsResult.Tables[0].DefaultView;
            pds.AllowPaging = true;
            pds.PageSize = 20;
            pds.CurrentPageIndex = Convert.ToInt32(this.labPage.Text) - 1;

            rplist.DataSource = pds;
            
            LabCountPage.Text = pds.PageCount.ToString();
            labPage.Text = (pds.CurrentPageIndex + 1).ToString();
            this.lbtnpritPage.Enabled = true;
            this.lbtnFirstPage.Enabled = true;
            this.lbtnNextPage.Enabled = true;
            this.lbtnDownPage.Enabled = true;

            if (pds.CurrentPageIndex < 1)
            {
                this.lbtnpritPage.Enabled = false; 
                this.lbtnFirstPage.Enabled = false; 
            }

            if (pds.CurrentPageIndex == pds.PageCount - 1)
            {
                this.lbtnNextPage.Enabled = false;
                this.lbtnDownPage.Enabled = false; 
            }

            rplist.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.labPage.Text = "1";
                this.lbSelect.Text = "";
                this.contrlRepeater(); 
            }
        }

        //货品查询
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.contrlRepeater(); 
        }

        //首页
        protected void lbtnFirstPage_Click(object sender, EventArgs e)
        {
            this.labPage.Text = "1"; 
            this.contrlRepeater(); 
        }

        //上一页
        protected void lbtnpritPage_Click(object sender, EventArgs e)
        {
            this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text) - 1);
            this.contrlRepeater(); 
        }

        //下一页
        protected void lbtnNextPage_Click(object sender, EventArgs e)
        {
            this.labPage.Text = Convert.ToString(Convert.ToInt32(labPage.Text) + 1);
            this.contrlRepeater(); 
        }

        //页尾
        protected void lbtnDownPage_Click(object sender, EventArgs e)
        {
            this.labPage.Text = this.LabCountPage.Text;
            this.contrlRepeater(); 
        }

        //删除与编辑
        protected void rplist_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string id = e.CommandArgument.ToString();

            switch (e.CommandName)
            {
                case "Select":
                    {
                        lbSelect.Text = id;
                        break;
                    }
                default:
                    {
                        break;
                    }
            }
        }

        //关闭
        protected void btClose_Click(object sender, EventArgs e)
        {
            lbSelect.Text = "";
        }

        //确认
        protected void btConfirm_Click(object sender, EventArgs e)
        {
            txt_product.Text = lbSelect.Text;
            lbSelect.Text = "";
        }
    }
}